package ipc.project;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;

public class ThreadSignOut implements Runnable {

	private Handler handler;
	private User user;
	private SharedPreferences settings;

	public ThreadSignOut(Handler h, User u, SharedPreferences s) {
		handler = h;
		user = u;
		settings=s;
	}

	public void run() {
		sendMessageToHandler(ProcessStatus.SIGN_UP_PROCESS, "Sign up start");
		if (!user.checkConnection()) {
			sendMessageToHandler(ProcessResult.CONNECTION_ERROR, "Server is disconnect");
			return;
		}
		if (user.signUp()) {
			sendMessageToHandler(ProcessResult.SIGN_UP_FINISH, "Sign up is succsess");
			sendMessageToHandler(ProcessResult.SIGN_IN_FINISH, "Sign in is succsess");
		}
		else {
			sendMessageToHandler(ProcessResult.SIGN_UP_ERROR, "Sign up fails");
			sendMessageToHandler(ProcessResult.SIGN_IN_ERROR, "Sign in fails");
			return;
		}
		user.saveData(settings);
	}

	private void sendMessageToHandler(int status, String body) {
		Message message = handler.obtainMessage(status, body);
		handler.sendMessage(message);
	}
	
}
